/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package MeteoCal.controller;

import MeteoCal.bean.exceptions.NonexistentEntityException;
import MeteoCal.bean.exceptions.PreexistingEntityException;
import MeteoCal.bean.exceptions.RollbackFailureException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import MeteoCal.entity.Notification;
import MeteoCal.entity.Receivednotification;
import MeteoCal.entity.ReceivednotificationPK;
import MeteoCal.entity.User;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.transaction.UserTransaction;

/**
 *
 * @author Daniele
 */
public class ReceivednotificationJpaController implements Serializable {

    public ReceivednotificationJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Receivednotification receivednotification) throws PreexistingEntityException, RollbackFailureException, Exception {
        if (receivednotification.getReceivednotificationPK() == null) {
            receivednotification.setReceivednotificationPK(new ReceivednotificationPK());
        }
        receivednotification.getReceivednotificationPK().setIdNotification(receivednotification.getNotification().getIdNotification());
        receivednotification.getReceivednotificationPK().setIdUser(receivednotification.getUser().getEmail());
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Notification notification = receivednotification.getNotification();
            if (notification != null) {
                notification = em.getReference(notification.getClass(), notification.getIdNotification());
                receivednotification.setNotification(notification);
            }
            User user = receivednotification.getUser();
            if (user != null) {
                user = em.getReference(user.getClass(), user.getEmail());
                receivednotification.setUser(user);
            }
            em.persist(receivednotification);
            if (notification != null) {
                notification.getReceivednotificationCollection().add(receivednotification);
                notification = em.merge(notification);
            }
            if (user != null) {
                user.getReceivednotificationCollection().add(receivednotification);
                user = em.merge(user);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findReceivednotification(receivednotification.getReceivednotificationPK()) != null) {
                throw new PreexistingEntityException("Receivednotification " + receivednotification + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Receivednotification receivednotification) throws NonexistentEntityException, RollbackFailureException, Exception {
        receivednotification.getReceivednotificationPK().setIdNotification(receivednotification.getNotification().getIdNotification());
        receivednotification.getReceivednotificationPK().setIdUser(receivednotification.getUser().getEmail());
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Receivednotification persistentReceivednotification = em.find(Receivednotification.class, receivednotification.getReceivednotificationPK());
            Notification notificationOld = persistentReceivednotification.getNotification();
            Notification notificationNew = receivednotification.getNotification();
            User userOld = persistentReceivednotification.getUser();
            User userNew = receivednotification.getUser();
            if (notificationNew != null) {
                notificationNew = em.getReference(notificationNew.getClass(), notificationNew.getIdNotification());
                receivednotification.setNotification(notificationNew);
            }
            if (userNew != null) {
                userNew = em.getReference(userNew.getClass(), userNew.getEmail());
                receivednotification.setUser(userNew);
            }
            receivednotification = em.merge(receivednotification);
            if (notificationOld != null && !notificationOld.equals(notificationNew)) {
                notificationOld.getReceivednotificationCollection().remove(receivednotification);
                notificationOld = em.merge(notificationOld);
            }
            if (notificationNew != null && !notificationNew.equals(notificationOld)) {
                notificationNew.getReceivednotificationCollection().add(receivednotification);
                notificationNew = em.merge(notificationNew);
            }
            if (userOld != null && !userOld.equals(userNew)) {
                userOld.getReceivednotificationCollection().remove(receivednotification);
                userOld = em.merge(userOld);
            }
            if (userNew != null && !userNew.equals(userOld)) {
                userNew.getReceivednotificationCollection().add(receivednotification);
                userNew = em.merge(userNew);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                ReceivednotificationPK id = receivednotification.getReceivednotificationPK();
                if (findReceivednotification(id) == null) {
                    throw new NonexistentEntityException("The receivednotification with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(ReceivednotificationPK id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Receivednotification receivednotification;
            try {
                receivednotification = em.getReference(Receivednotification.class, id);
                receivednotification.getReceivednotificationPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The receivednotification with id " + id + " no longer exists.", enfe);
            }
            Notification notification = receivednotification.getNotification();
            if (notification != null) {
                notification.getReceivednotificationCollection().remove(receivednotification);
                notification = em.merge(notification);
            }
            User user = receivednotification.getUser();
            if (user != null) {
                user.getReceivednotificationCollection().remove(receivednotification);
                user = em.merge(user);
            }
            em.remove(receivednotification);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Receivednotification> findReceivednotificationEntities() {
        return findReceivednotificationEntities(true, -1, -1);
    }

    public List<Receivednotification> findReceivednotificationEntities(int maxResults, int firstResult) {
        return findReceivednotificationEntities(false, maxResults, firstResult);
    }

    private List<Receivednotification> findReceivednotificationEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Receivednotification.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Receivednotification findReceivednotification(ReceivednotificationPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Receivednotification.class, id);
        } finally {
            em.close();
        }
    }

    public int getReceivednotificationCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Receivednotification> rt = cq.from(Receivednotification.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
